<?php

namespace App\Http\Controllers\Game\Other\Mail;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Game\Other\Mail\Server\StoreRequest;
use App\Http\Requests\Game\Other\Mail\Server\UpdateRequest;
use App\Http\Resources\Game\Other\Email\Server\DatasResource;
use App\Http\Resources\Common\DataResource;
use App\Models\Game\Mail\Server as Mail;
use App\Services\Game\Other\Mail\ServerService as MailService;
use App\Services\Postman\GameService as PostmanGameService;

class ServerController extends Controller
{

    /**
     * 获取数据列表
     */
    public function getList(Request $request)
    {
        list ($collection, $total) = MailService::make()->getList($request);

        return DatasResource::make($collection)->setPaginateTotal($total);
    }

    /**
     * 创建数据
     */
    public function store(StoreRequest $request)
    {
        $mail = MailService::make()->store($request);

        return new DataResource($mail);
    }

    /**
     * 更新数据
     */
    public function update(UpdateRequest $request)
    {
        $id = $request->input('id');

        $mail = MailService::make()->update($id, $request);

        return new DataResource($mail);
    }

    /**
     * 发送邮件
     */
    public function sendMail(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        $mail = Mail::findOrFail($id);

        PostmanGameService::post(
            PostmanGameService::URI_SEND_SERVER_MAIL, ['mail_id' => $mail->id]
        );

        return new DataResource(null);
    }

    /**
     * 删除数据
     */
    public function destroy(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        $mail = Mail::findOrFail($id);
        $mail->delete();

        return new DataResource($mail);
    }
}
